<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of EducationAdminController
 *
 * @author dro
 */

namespace Gairal\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Gairal\CoreBundle\Form\EducationType;
use Gairal\CoreBundle\Entity\Education;

class EducationAdminController extends Controller {
    
    public function indexAction() {
        
        $listEducation = $this->getDoctrine()
                ->getManager()
                ->getRepository('GairalCoreBundle:Education')
                ->findAll();

        return $this->render('GairalAdminBundle:Education:index.html.twig', array(
                    'action' => 'Admin - Education',
                    'listEducation' => $listEducation
        ));
    }
    
    public function addAction() {
        
        $education = new Education();
        $form = $this->createForm(new EducationType(), $education);
        $options = $form->get('image')->getConfig()->getOptions();
        $choices = $options['choice_list']->getChoices();

        // On récupère la requête
        $request = $this->get('request');
        // On vérifie qu'elle est de type POST
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager(); 
                $em->persist($education);
                $em->flush();
                
                $this->get('session')->getFlashBag()->add('info', 'Education '. $education->getName() .' added');
                return $this->redirect($this->generateUrl('AdminEducation'));
            }
            else
                $this->get('session')->getFlashBag()->add('error', $form->getErrorsAsString());
        }

        return $this->render('GairalAdminBundle:Education:add.html.twig', array(
                    'action' => 'Admin - Education - Add',
                    'form' => $form->createView(),
                    'image_choices' => $choices
        ));
    }
    
    public function removeAction(Education $education) {
        if ($education != null) {
            $em = $this->getDoctrine()
                    ->getManager();
            $em->remove($education);
            $em->flush();
            $this->get('session')->getFlashBag()->add('info', 'Education "' . $education->getName() . '" removed');
        }
        else
            $this->get('session')->getFlashBag()->add('error', 'Education not removed, error in url');

        return $this->redirect($this->generateUrl('AdminEducation'));
    }
    
    public function modifyAction(Education $education) {
        $form = $this->createForm(new EducationType(), $education);
        $options = $form->get('image')->getConfig()->getOptions();
        $choices = $options['choice_list']->getChoices();

        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            if ($form->isValid()) {

                $em = $this->getDoctrine()->getManager();
                $em->persist($education);
                $em->flush();

                $this->get('session')->getFlashBag()->add('info', 'Education "' . $education->getName() . '" modified');
                return $this->redirect($this->generateUrl('AdminEducation'));
            }
            else
                $this->get('session')->getFlashBag()->add('error', $form->getErrorsAsString());
        }
        return $this->render('GairalAdminBundle:Education:modify.html.twig', array(
                    'action' => 'Admin - Education - modify - '. $education->getName(),
                    'form' => $form->createView(),
                    'Education' => $education,
                    'image_choices' => $choices
        ));
    }
}

?>
